package com.awspaas.user.apps.sts.HttpApi;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.HandlerType;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.server.bind.annotation.Param;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.service.response.StringResponse;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.sts.util.MiddleLogUtil;
import jodd.util.StringUtil;
import org.apache.commons.collections.map.HashedMap;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Map;

@Controller(type = HandlerType.OPENAPI, apiName = "STS API", desc = "STS回调接口")
public class StsBtnWebApi {
    @Mapping(value = "Sts.Updata")
    public StringResponse UpdataLogTable(
            @Param(value = "applicationName", desc = "系统标识", required = true) String applicationName,
            @Param(value = "taskinstID", desc = "任务实例ID", required = true) String taskinstID,
            @Param(value = "processInstID", desc = "流程实例ID", required = true) String processInstID,
            @Param(value = "process_def_ID", desc = "流程定义ID", required = true) String process_def_ID,
            @Param(value = "buttonID", desc = "三方按钮ID", required = true) String buttonID,
            @Param(value = "bpmButtonID", desc = "BPM按钮ID", required = true) String bpmButtonID,
            @Param(value = "rejectionType", desc = "驳回类型", required = false) String rejectionType,
            @Param(value = "buttonType", desc = "按钮类型", required = false) String buttonType,
            @Param(value = "buttonStatus", desc = "按钮状态", required = true) String buttonStatus,
            @Param(value = "receivingResults", desc = "接收结果", required = false) String receivingResults,
            @Param(value = "createLogTimeStamp", desc = "插入日志时间戳", required = true) String createLogTimeStamp,
            @Param(value = "businessIndex", desc = "业务线索ID", required = false) String businessIndex,
            @Param(value = "businessStatusID", desc = "业务状态ID", required = false) String businessStatusID,
            @Param(value = "projectID", desc = "项目ID", required = false) String projectID,
            @Param(value = "productID", desc = "产品ID", required = false) String productID,
            @Param(value = "contractID", desc = "合同ID", required = false) String contractID,
            @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo
    ){

        StringResponse stringResponse = new StringResponse();
        Map<String,String> paramsMap = new HashedMap();
        paramsMap.put("APPLICATIONNAME", applicationName);
        paramsMap.put("TASKINSTID", taskinstID);
        paramsMap.put("PROCESSINSTID", processInstID);
        paramsMap.put("PROCESSDEFID", process_def_ID);
        paramsMap.put("BUTTONID", buttonID);
        paramsMap.put("BPMBUTTONID", bpmButtonID);
        paramsMap.put("REJECTIONTYPE", rejectionType);
        paramsMap.put("BUTTONTYPE", buttonType);
        paramsMap.put("BUTTONSTATUS", buttonStatus);
        paramsMap.put("BUSINESSRESULT", receivingResults);
        paramsMap.put("CREATELOGTIMESTAMP", createLogTimeStamp);
        if(null!=extInfo && !extInfo.isEmpty()) {
            paramsMap.put("EXTINFO", extInfo.toString());
        }
        MiddleLogUtil middleLogUtil = new MiddleLogUtil();
        String updateBtnLogState = middleLogUtil.UpdateBtnLog(paramsMap);
        stringResponse.setMsg(updateBtnLogState);
        if (updateBtnLogState.equals("updateNULL")) {
            stringResponse.setData("日志不存在，更新失败");
        } else if (updateBtnLogState.equals("insertOK")) {
            stringResponse.setData("已存在已执行结束的日志，新增日志成功");
        } else if (updateBtnLogState.equals("insertERROR")) {
            stringResponse.setData("已存在已执行结束的日志，新增日志出现异常，新增失败");
        } else if (updateBtnLogState.equals("updateOK")) {
            stringResponse.setData("更新日志成功");
        } else if (updateBtnLogState.equals("updateERROR")) {
            stringResponse.setData("更新日志出现异常，更新失败");
        }
        return stringResponse;
    }
}
